<?php

namespace App\Http\Controllers\Admin;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\youqingRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class YouqingController extends Controller
{

    public function getIndex(Request $request)
    {
        //每页显示条数
        $num = $request->input('num',2);

        //检测是否提交查询关键字
        if($request->has('keywords')){
             //分页查询 select * from user where username like '%admin%';
            $cates = DB::table('youqing')->where('title','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $cates = DB::table('youqing')->paginate($num);
        }

        //提取参数 分配到页面中
        $data = $request->except('page');


        //解析模板 分配数据
        return view('Admin.youqing.index',['users'=>$cates,'data'=>$data]);
    }



/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
       return view('Admin.youqing.add');

    }

    //添加
     public function postInsert(Request $request){
     
           //除了某个不要
        $data=$request->except('_token');

        $this->validate($request,[
            'title'=>'required|max:255',
            'logo'=>'required',
            'dizhi'=>'required',
            
          
            ],[
                'title.required'=>'标题必须填写',
                'logo.required'=>'请上传图片',
                'dizhi.required'=>'地址必须填写',
                
               
            ]);




         //检测是否有文件上传
    
        $data['logo']=$this->upload($request);
    
      //dd($data);

      //把数据插到数据库
        $res=DB::table('youqing')->insertGetId($data);
       //dd($res);
        if($res){

            return redirect('admin/youqing/index')->with('success','式神添加成功');
        }else{
            return back()->with('error','式神添加失败');
        }
 }

  //文件上传操作
    public function upload($request){
       
        //检测是否有文件上传
        if($request->hasFile('logo')){
             
             //随机名
            $name=md5(time()+rand(1,999));

            //后缀名
            $su=$request->file('logo')->getClientOriginalExtension();
           $request->file('logo')->move('./public/uploads/youqing/',$name.'.'.$su);
            return './public/uploads/youqing/'.$name.'.'.$su;
           


        }

   
    }

      //友情删除
    public function getDel(Request $request){

        //获取
       $id=$request->input('id');
       //dd($id);
       //执行
       $res=DB::table('youqing')->where('id',$id)->delete();

        echo $res;

    }
 //用户修改
    public function getEdit($id){

       //根据id查信息
        $users=DB::table('youqing')->where('id',$id)->first();
    
        //解析模板
       return view('Admin.youqing.edit',['users'=>$users,'id'=>$id]);
    }


    //执行用户数据的修改
   public function postUpdate(Request $request){

        $data=$request->except('_token');   
       
         if($request->hasFile('logo')){
            //调用方法进行logo头像上传
         $data['logo']=$this->upload($request);
         //dd($data);
      }
        
      // dd($data['id']);
        
       //执行数据修改
        $res=DB::table('youqing')->where('id',$data['id'])->update($data);
      

       if($res){

             //重定向到peizhi/peizhi
            return redirect('admin/youqing/index')->with('success','式神修改成功');
        }else{
            //回到上个页面
            return back()->with('error','式神修改失败');

        }

        
    }

   
}
